package com.yiwei.ydd.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.yiwei.ydd.R;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.constant.UserInfoGlobal;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.V19FrameLayout;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyRefereeActivity extends BaseActivity {


    @BindView(R.id.img_top_bg)
    SuperButton imgTopBg;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_invite_code)
    TextView txtInviteCode;
    @BindView(R.id.layout_bottom)
    LinearLayout layoutBottom;
    @BindView(R.id.btn_invite)
    SuperButton btnInvite;
    @BindView(R.id.img_icon)
    ImageView imgIcon;
    private Unbinder unbiner;
//我的推荐人

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_referee);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        getUserInviteInfo();
//        getUserInfo();
    }

    public void getUserInviteInfo() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();

        Api.api_service.getUserInviteInfo().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    Glide.with(this).load(result.datas.icon).asBitmap().error(R.mipmap.img_default_head).into(imgIcon);
                    txtName.setText(result.datas.username);
                    txtInviteCode.setText("好友邀请码："+result.datas.invite_code);
                });
    }

//    public void getUserInfo() {
//        if (!Network.checkNetwork(this)) {
//            ToastUtil.show(this, "网络连接失败");
//            return;
//        }
//        loadingDialog.show();
//        Api.api_service.getUserInfo().compose(RxLifeUtil.NocheckOn(this)).
//                doFinally(() -> {
//                    try {
//                        loadingDialog.dismiss();
//                    } catch (NullPointerException e) {
//
//                    }
//                }).
//                subscribe(result -> {
//                    if (result.getStatus().equals("00")) {
//                        UserInfoGlobal.saveUserInfo(result.datas);
//                        Glide.with(this).load(result.datas.icon).asBitmap().error(R.mipmap.img_default_head).into(imgIcon);
//                        txtName.setText(result.datas.username);
//                        txtInviteCode.setText("好友邀请码：" + result.datas.invite_code);
//
//                    } else {
//
//                    }
//
//                });
//    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.txt_right, R.id.btn_invite, R.id.btn_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_right:
                Util.startActivity(this, ChatCenterActivity.class);
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_invite:
                Util.startActivity(this, ShareIndexActivity.class);
                break;
        }
    }
}
